var a = require("../../utils/config.js");

Page({
    data: {
        status: 0,
        signedPage: 1,
        takedPage: 1,
        finishedPage: 1,
        signedList: [],
        takedList: [],
        finishedList: [],
        isSignedEnd: !1,
        isTaskedEnd: !1,
        isFinishedEnd: !1,
        signedNum: 0,
        takedNum: 0,
        finishedNum: 0
    },
    onLoad: function(a) {
        1 == a.type && this.setData({
            status: 1
        });
    },
    onReady: function() {},
    onShow: function() {
        this.onPullDownRefresh(), this.getTaskInfo();
    },
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {
        var a = this;
        0 == a.data.status ? a.data.signedPage = 1 : 1 == a.data.status ? a.data.takedPage = 1 : 2 == a.data.status && (a.data.finishedPage = 1), 
        a.loadDataSource(), a.getTaskInfo();
    },
    onReachBottom: function() {
        var a = this;
        if (0 == a.data.status) {
            if (a.data.isSignedEnd) return void wx.showToast({
                title: "到底啦",
                icon: "none"
            });
            a.data.signedPage++;
        } else if (1 == a.data.status) {
            if (a.data.isTaskedEnd) return void wx.showToast({
                title: "到底啦",
                icon: "none"
            });
            a.data.takedPage++;
        } else if (2 == a.data.status) {
            if (a.data.isFinishedEnd) return void wx.showToast({
                title: "到底啦",
                icon: "none"
            });
            a.data.finishedPage++;
        }
        a.loadDataSource();
    },
    onShareAppMessage: function() {},
    changeTab: function(a) {
        var t = this, e = a.currentTarget.dataset.status;
        t.setData({
            status: e
        }), 0 == e && 0 == t.data.signedList.length ? t.loadDataSource() : 1 == e && 0 == t.data.takedList.length ? t.loadDataSource() : 2 == e && 0 == t.data.finishedList.length && t.loadDataSource();
    },
    loadDataSource: function() {
        var t = this, e = wx.getStorageSync("userId"), s = 1;
        0 == t.data.status ? 1 == (s = t.data.signedPage) && (t.setData({
            isSignedEnd: !1
        }), t.data.signedList = []) : 1 == t.data.status ? 1 == (s = t.data.takedPage) && (t.setData({
            isTaskedEnd: !1
        }), t.data.takedList = []) : 2 == t.data.status && 1 == (s = t.data.finishedPage) && (t.setData({
            isFinishedEnd: !1
        }), t.data.finishedList = []);
        var d = {
            userId: e,
            page: s <= 0 ? 1 : s,
            rows: 10,
            status: t.data.status
        };
        console.log(d), wx.request({
            url: a.urlList.API_TASK_GET_MY_TASK_LIST,
            method: "GET",
            header: {
                contentType: "application/json"
            },
            data: d,
            success: function(a) {
                200 == a.data.status ? (0 == t.data.status ? t.setData({
                    signedList: t.data.signedList.concat(a.data.data)
                }) : 1 == t.data.status ? t.setData({
                    takedList: t.data.takedList.concat(a.data.data)
                }) : 2 == t.data.status && t.setData({
                    finishedList: t.data.finishedList.concat(a.data.data)
                }), a.data.data.length < 10 && (0 == t.data.status ? (t.data.signedPage--, t.setData({
                    isSignedEnd: !0
                })) : 1 == t.data.status ? (t.data.takedPage--, t.setData({
                    isTaskedEnd: !0
                })) : 2 == t.data.status && (t.data.finishedPage--, t.setData({
                    isFinishedEnd: !0
                })))) : wx.showToast({
                    title: a.data.message,
                    icon: "none"
                }), wx.stopPullDownRefresh();
            }
        });
    },
    goToMyTaskDetail: function(a) {
        wx.navigateTo({
            url: "myTaskDetail?id=" + a.currentTarget.dataset.id
        });
    },
    finishTask: function(t) {
        var e = this;
        if (1 == e.data.status) {
            var s = e.data.takedList[t.currentTarget.dataset.index];
            wx.showModal({
                title: "系统提示",
                content: "是否确认完成任务？",
                success: function(d) {
                    d.confirm && (s.acceptanceRequirements.indexOf("照片") >= 0 || s.acceptanceRequirements.indexOf("签到") >= 0 || s.acceptanceRequirements.indexOf("文字报告") >= 0 ? wx.navigateTo({
                        url: "taskReport?id=" + t.currentTarget.dataset.id
                    }) : wx.request({
                        url: a.urlList.API_TASK_FINISH_TASK,
                        method: "POST",
                        data: {
                            id: t.currentTarget.dataset.id,
                            userId: wx.getStorageSync("userId")
                        },
                        header: {
                            contentType: "application/json"
                        },
                        success: function(a) {
                            wx.hideLoading(), 200 == a.data.status ? (wx.showToast({
                                title: "完成任务提交成功"
                            }), e.onPullDownRefresh()) : wx.showToast({
                                title: a.data.message,
                                icon: "none"
                            });
                        }
                    }));
                }
            });
        }
    },
    cancelSign: function(t) {
        var e = this, s = wx.getStorageSync("userId");
        wx.showLoading({
            mask: !0
        });
        var d = {
            userId: s,
            id: t.currentTarget.dataset.id
        };
        wx.request({
            url: a.urlList.API_TASK_CANCEL_SIGN,
            method: "POST",
            data: d,
            header: {
                contentType: "application/json"
            },
            success: function(a) {
                wx.hideLoading(), 200 == a.data.status ? (wx.showToast({
                    title: "取消报名成功"
                }), e.onPullDownRefresh()) : wx.showToast({
                    title: a.data.message,
                    icon: "none"
                });
            }
        });
    },
    getTaskInfo: function() {
        var t = this, e = wx.getStorageSync("userId");
        wx.request({
            url: a.urlList.API_TASK_GET_TASK_INFO,
            method: "GET",
            data: {
                userId: e
            },
            success: function(a) {
                200 == a.data.status && t.setData({
                    signedNum: a.data.data.signedNum,
                    takedNum: a.data.data.takedNum,
                    finishedNum: a.data.data.finishedNum
                });
            }
        });
    }
});